﻿using BL_Client.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class Charges : System.Web.UI.Page
    {
        static BL_Client.IBL bl = new BL_Client.BL();
        static User user;

        public Charges()
        {
            user = bl.GetUserByEmail(User.Identity.Name);
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (userIsAdmin(user)) //check the role of the user
                this.Page.MasterPageFile = "~/Admin.Master";
            else
                this.Page.MasterPageFile = "~/AvBait.Master";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (userIsAdmin(user))
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#ChargesTableContainer').jtable({
                                                    title: 'Table of Charges',
                                                    actions: {
                                                        listAction: '/Charges.aspx/ChargesList',
                                                        createAction: '/Charges.aspx/CreateCharge',
                                                        updateAction: '/Charges.aspx/UpdateCharge',
                                                        deleteAction: '/Charges.aspx/DeleteCharge'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        Name: {
                                                            title: 'Charge Name',
                                                            width: '40%'
                                                        },
                                                        Code: {
                                                            title: 'Charge Code',
                                                            width: '30%'
                                                        },
                                                        Amount: {
                                                            title: 'Amount',
                                                            width: '30%'
                                                        }
                                                    }
                                                });

                                                $('#ChargesTableContainer').jtable('load');
                                            });
                                        </script>";
            else
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#ChargesTableContainer').jtable({
                                                    title: 'Table of Charges',
                                                    actions: {
                                                        listAction: '/Charges.aspx/ChargesList'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        Name: {
                                                            title: 'Charge Name',
                                                            width: '40%'
                                                        },
                                                        Code: {
                                                            title: 'Charge Code',
                                                            width: '30%'
                                                        },
                                                        Amount: {
                                                            title: 'Amount',
                                                            width: '30%'
                                                        }
                                                    }
                                                });

                                                $('#ChargesTableContainer').jtable('load');
                                            });
                                        </script>";
        }

        [WebMethod(EnableSession = true)]
        public static object ChargesList()//int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                List<Charge> charges = bl.GetAllCharges().ToList();

                //Return result to jTable
                return new { Result = "OK", Records = charges, TotalRecordCount = charges.Count };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object CreateCharge(Charge record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.AddCharge(record))
                {
                    Charge addedCharge = record;
                    return new { Result = "OK", Record = addedCharge };
                }
                else
                {
                    return new { Result = "ERROR", Message = "Profession insertion error" };
                }
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object UpdateCharge(Charge record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                bl.UpdateCharge(record);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteCharge(int ID)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                bl.RemoveCharge(ID);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }

        }

        private static bool userIsAdmin(User user)
        {
            return (user.Role == Role.Admin);
        }
    }
}